/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */

static char RCSId[]="$Id: EditMatrix.m,v 1.5 1993/05/18 03:55:01 davis Exp $";


#import <appkit/TextFieldCell.h>
#import "EditMatrix.h"

@interface EditMatrix (Private)

- (BOOL)_incrementCount:sender;

@end


@implementation EditMatrix

- initFrame:(const NXRect *)frameRect
{
    [super initFrame:frameRect];
    editableCells = NO;

    return self;
}


- setEditableCells:(BOOL)aCond
{
    editableCells = aCond;
    return self;
}


- (BOOL)editableCells
{
    return editableCells;
}


- mouseDown:(NXEvent *)theEvent
{
    /*  
     *  If there's a double-click, and if editableCells is turned on, 
     *  allow the cell to be edited.
     */
    
    if (editableCells && theEvent->data.mouse.click == 2)
	[editingCell = [self selectedCell] setEditable:YES];

    /*  Clicking outside the cell should end editing  */

    if (editingCell && [window makeFirstResponder:window] )
	[window endEditingFor:nil];

    return [super mouseDown:theEvent];
}



- textDidEnd:textObject endChar:(short unsigned)whyEnd
{
    [editingCell setEditable:NO];
    editingCell = nil;

    return [super textDidEnd:textObject endChar:whyEnd];
}



- (BOOL)multipleCellsSelected
{
    selectedCount = 0;
    
    [self sendAction:@selector(_incrementCount:) to:self forAllCells:NO];
    return (selectedCount > 1);
}


- selectedCell
{
    if ([self multipleCellsSelected])
	return nil;
    else
	return [super selectedCell];
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end


@implementation EditMatrix (Private)

- (BOOL)_incrementCount:sender
{
    return (++selectedCount < 2);
}


@end
